
//规则条件列表js代码块
var MenuPage = {
		
		//初始化一些东西
		init : function(){
			
			//点击图标触发事件
			$("#chooseIconWindow ul li").click(function(){
				
				var jqSpan = $(this).find("span");
				var iconName = jqSpan.attr("iconName");
				
				$("#iconInput").val(iconName);
				
				$("#showIconSpan").html('<i class="icon '+iconName+'">');
				
				$('#chooseIconWindow').modal("hide");

			});
			
			//初始化菜单
			MenuPage.initMenuTree();
				
		},
		
		//重新在文本框生成编号
		createMenuNoForInput : function(){
			$("#menuNoInput").val(MenuPage.createMenuNo());
		},
		
		//创建菜单编号
		createMenuNo : function(){
			return "M" +CommonUtil.createDateNo();
		},
		
		//初始化菜单树
		initMenuTree : function(){
			
			var requestUrl ="/menu/getUserMenu";
			
			 ajaxRequestAsync(requestUrl , {} , function(rst){
				 
				 var data = rst.data;
				 
				 if(!data || data.length == null) return;
				 
				 var menuTxt = MenuPage.createMenuTxt(data);
				 
				 if(menuTxt != ""){
					 var jqTree = $("#menuLayerId");
					 jqTree.html(menuTxt)
					 jqTree.find("li").addClass("open in");
					 jqTree.find("ul").show();
					 $("#addMenuBtnId").hide();
				 }else{
					 $("#addMenuBtnId").show();
				 }
				 
			 });
			
			
		},
		
		//改变菜单类型
		changeMenuType : function(){
			
			var value = $("#menuTypeSelect").val();
			
			if(value == 1){
				$("#menuNameLabel").html("按钮名称");
				$("#menuNoLabel").html("按钮编号");
				$("#menuUrlLabel").html("接口地址");
				$("#menuInfoDiv").hide();
			}else{
				$("#menuNameLabel").html("菜单名称");
				$("#menuNoLabel").html("菜单编号");
				$("#menuUrlLabel").html("链接地址");
				$("#menuInfoDiv").show();
			}
			
		},
		
		//创建菜单
		createMenuTxt : function(dataList){
			
			var txt = '';
			
			var isRootNode = false;
			
			for(var i = 0 ; i < dataList.length ; i ++){
				
				var item = dataList[i];
				
				//根基诶单
				isRootNode = !item.parentMenuId || item.parentMenuId == null;
				
				var params = {"name": item.name , "url" : item.url , "icon" : item.icon ,  "menuNo":item.menuNo , "type": item.type , "id": item.id+"" , "parentId" : isRootNode ? undefined : item.parentMenuId + "" };
				
				var itemTxt = MenuPage.createNode(params);
				
				var children = item.children;
				
				if(children && children.length > 0){
					
					//父节点的+-号不隐藏了
					itemTxt = itemTxt.replace('style="display:none;"' , "");
					
					var childrenTxt = '<ul>' + MenuPage.createMenuTxt(children) + '</ul></li>';
					
					itemTxt = itemTxt.replace("</li>" , "") + childrenTxt;
					
				}
				
				txt += itemTxt;
			}
			
			return txt;
			
		},
		
		//打开创建窗口
		openCreateWindow : function(tag , operateType , type ,  operateMenuId){
			
			//先清空内容
			$("#addMenuWindow").find("input").val("");
			
			operateMenuId = operateMenuId + "";
			
			//类型可用
			$("#menuTypeSelect").val(type).removeAttr("disabled").change();
			
			//新增菜单弹窗
			if(operateType == 0){
				MenuPage.createMenuNoForInput(); //在文本框赋值编号
				$("#addMenuTitle").html("新增根节点").attr("operateType" , operateType).attr("type" , type).attr("operateMenuId" , operateMenuId);
			}else if(operateType == 1){
				MenuPage.createMenuNoForInput(); //在文本框赋值编号
				$("#addMenuTitle").html("新增同级节点").attr("operateType" , operateType).attr("type" , type).attr("operateMenuId" , operateMenuId);
			}else if(operateType == 2){
				MenuPage.createMenuNoForInput(); //在文本框赋值编号
				$("#addMenuTitle").html("新增子节点").attr("operateType" , operateType).attr("type" , type).attr("operateMenuId" , operateMenuId);
			}else if(operateType == 3){
				
				//编辑的时候 , 类型不可以修改
				$("#menuTypeSelect").attr("disabled" , "disabled");
				
				$("#addMenuTitle").html("编辑").attr("operateType" , operateType).attr("type" , type).attr("operateMenuId" , operateMenuId);
				
				var jqItem = $("#" + operateMenuId);
				var name = jqItem.attr("name");
				var icon = jqItem.attr("icon");
				var url = jqItem.attr("url");
				var menuNo = jqItem.attr("menuNo");
				var type = jqItem.attr("type");
				
				$("#menuNameInput").val(name);
				$("#menuUrlInput").val(url);
				$("#iconInput").val(icon);
				$("#menuNoInput").val(menuNo);
				$("#menuTypeSelect").val(type).change();
				
			}
			
			$("#addMenuWindow").modal();
			
		},
		
		//新增菜单
		addMenu:function(params){
			var requestUrl = "/menu/addMenu";
			var rst = ajaxRequestByJSONParams(requestUrl , params);
			return rst.data + "";
		},
		
		//获取菜单参数
		getMenuParams : function(){
			
			//参数
			var params = {};
			params.name = $.trim($("#menuNameInput").val());
			params.url = $.trim($("#menuUrlInput").val());
			params.icon = $.trim($("#iconInput").val());
			params.type = $.trim($("#menuTypeSelect").val());
			params.menuNo = $.trim($("#menuNoInput").val());
			
			return params;
		},
		
		//保存菜单
		saveMenu : function(){
		
			//弹窗上的数据
			var operateType = $("#addMenuTitle").attr("operateType"); // 0 : 新增根节点 1:新增同级节点 , 2:新增子节点 3, :编辑
			var operateMenuId = $("#addMenuTitle").attr("operateMenuId") + "";
			var type = $("#addMenuTitle").attr("type") + "";
			
			var params = MenuPage.getMenuParams();
			
			if(params.name == ""){
		 		new $.zui.Messager('名称不能为空', {icon:'times',type: 'danger',placement: 'center',time:'2000'}).show();
				return;
			}
			
			if(params.menuNo == ""){
		 		new $.zui.Messager('编号不能为空', {icon:'times',type: 'danger',placement: 'center',time:'2000'}).show();
				return;
			}
			
			//若是追加根节点
			if(operateType == 0){
				
				//先调用接口
				var id = MenuPage.addMenu(params);
				
				params.id = id + "";
				
				var txt = MenuPage.createNode(params);
				
				$("#menuLayerId").append(txt);
				
			}
			//若是追加同级节点
			else if(operateType == 1){
				
				var parentId = $("#" + operateMenuId).attr("parentId");
				
				params.parentMenuId = parentId ? parentId : null;
				
				//先调用接口
				var id = MenuPage.addMenu(params);
				
				params.id = id + "";
				params.parentId = parentId;
				
				var txt = MenuPage.createNode(params);
				
				if(parentId && parentId != null){
					$("#" +parentId ).append(txt);
				}else{
					$("#" + operateMenuId).after(txt);
				}
			}
			//若是追加子节点
			else if(operateType == 2){
			
				params.parentMenuId = operateMenuId;
				
				$("#" + operateMenuId).children("a").find(".icon-file-o").removeClass("icon-file-o").addClass("icon-th-list");
				
				var id = MenuPage.addMenu(params);
				
				params.id = id + "";
				params.parentId = operateMenuId;
				
				var txt = MenuPage.createNode(params);
				
				var hasUl = $("#" + operateMenuId).find("ul").length > 0;
				
				if(hasUl){
					$("#" + operateMenuId).children("ul").append(txt).show();
				}else{
					$("#" + operateMenuId).append('<ul>' + txt + '</ul>').children("ul").show();
				}
				
			}
			//若是编辑
			else if(operateType == 3) {
				
				var requestUrl = "/menu/updateMenu";
				
				params.id = operateMenuId;
				
				var rst = ajaxRequestByJSONParams(requestUrl , params);
				
				if(rst.code != 200){
					new $.zui.Messager(rst.message, {icon:'times',type: 'danger',placement: 'center',time:'2000'}).show();
					return;
				}
				
				if(type == 0){
					$("#" + operateMenuId).children("a").children("i").replaceWith('<i class="icon '+params.icon+'"></i>');
					$("#" + operateMenuId)
					.attr("name" , params.name).attr("icon" , params.icon).attr("url" , params.url).attr("menuNo" , params.menuNo).attr("type" , params.type)
					.children("a").children("span").html(params.name);
				}else{
					$("#" + operateMenuId)
					.attr("name" , params.name).attr("menuNo" , params.menuNo).attr("type" , params.type)
					.children("a").children("span").html(params.name);
				}
				
			}
			
			//隐藏
			$("#addMenuBtnId").hide();
			
			$('#addMenuWindow').modal("hide");
			
		},
		
		//创建节点
		createNode : function(params) {
			
			var name = params.name;
			var id = params.id + "";
			var icon = params.icon;
			var url = params.url;
			var parentId = params.parentId;
			var type = params.type;
			var menuNo = params.menuNo;
			
			//转化为字符串 , 不然传入到参数中又会变为长整型而失去精度
			id = id + "";
			
			icon = !icon || icon == "" ? "icon-file-o" : icon;
			
			var nameTxt = name && name != "" ? ' name="'+name+'" ' : ""; 
			var iconTxt = ' icon="'+icon+'" ';
			var urlTxt = url && url != ""  ? ' url="'+url+'" ' : ""; 
			var idTxt = id && id != "" ? ' id="'+id+'" ' : ""; 
			var parentIdTxt = parentId && parentId != "" ? ' parentId="'+parentId+'" ' : ""; 
			var typeTxt = type && type != "" ? ' type="'+type+'" ' : ""; 
			var menuNoTxt = menuNo && menuNo != "" ? ' menuNo="'+menuNo+'" ' : ""; 
			
			var menuInfoTxt = idTxt + nameTxt + iconTxt + urlTxt + typeTxt + menuNoTxt + parentIdTxt;
			
			var txt = '';
			
			if(type == 0){
				
				txt = '<li '+menuInfoTxt+'><i class="list-toggle icon"  onclick="MenuPage.toggleChildMenu(&quot;'+id+'&quot;)" style="display:none;"></i><a href="javascript:void(0);"><i class="icon '+icon+'"></i>&nbsp;<span>'+name+'</span></a>&nbsp;'+
							'<i class="icon icon-plus-sign" style="color:blue;cursor:pointer;" title="添加同级节点" onclick="MenuPage.openCreateWindow(this , 1 , 0 , &quot;'+id+'&quot;)"></i>&nbsp;'+
							'<i class="icon icon-plus-sign" style="color:orange;cursor:pointer;" title="添加子节点" onclick="MenuPage.openCreateWindow(this , 2 , 0 , &quot;'+id+'&quot;)"></i>&nbsp;'+
							'<i class="icon icon-edit" title="编辑" style="cursor:pointer;" onclick="MenuPage.openCreateWindow(this , 3 , 0 , &quot;'+id+'&quot;)"></i>&nbsp;'+
							'<i class="icon icon-trash" title="删除" style="cursor:pointer;" onclick="MenuPage.openDeleteWindow(&quot;'+id+'&quot;)"></i>';

			}else{
				txt = '<li '+menuInfoTxt+'><i class="list-toggle icon" onclick="MenuPage.toggleChildMenu(&quot;'+id+'&quot;)"  style="display:none;"></i><a href="javascript:void(0);"><button style="font-size:12px;" type="button" disabled="disabled">按钮</button>&nbsp;<span>'+name+'</span></a>&nbsp;'+
				'<i class="icon icon-plus-sign" style="color:blue;cursor:pointer;" title="添加同级节点" onclick="MenuPage.openCreateWindow(this , 1 , 1 , &quot;'+id+'&quot;)"></i>&nbsp;'+
				'<i class="icon icon-plus-sign" style="color:orange;cursor:pointer;" title="添加子节点" onclick="MenuPage.openCreateWindow(this , 2 , 1 , &quot;'+id+'&quot;)"></i>&nbsp;'+
				'<i class="icon icon-edit" title="编辑" style="cursor:pointer;" onclick="MenuPage.openCreateWindow(this , 3 , 1 , &quot;'+id+'&quot;)"></i>&nbsp;'+
				'<i class="icon icon-trash" title="删除" style="cursor:pointer;" onclick="MenuPage.openDeleteWindow(&quot;'+id+'&quot;)"></i>';
			}
			
			//上面的图标展示
			if(parentId){
				$("#" + parentId).children(".list-toggle.icon").show();
			}
			
			return 	txt;
		},
		
		//打开隐藏菜单
		toggleChildMenu : function(id){
			$("#" + id).find("ul").toggle();
		},
		
		//打开删除弹窗
		openDeleteWindow : function(id){
			$("#deleteMenuWindow").attr("deleteMenuId", id + "").modal();
		},
		
		//删除菜单
		deleteMenu: function(){
			
			var id = $("#deleteMenuWindow").attr("deleteMenuId");
			
			var requestUrl = "/menu/deleteMenuById";
			
			var params = {"id" : id + ""};
			
			ajaxRequestAsync(requestUrl , params , function(rst){
				
				if(rst.code == 200){
					
					//若一个都没有了 , 则展示新增按钮
					if($("#menuLayerId").find("li").length == 0){
						$("#addMenuBtnId").show();
					}
					
					$("#" + id).remove();
					
					$("#deleteMenuWindow").modal("hide");
					
					return;
				}
				
				new $.zui.Messager(rst.message, {icon:'times',type: 'danger',placement: 'center',time:'2000'}).show();
				
			});
			
		
		}
	
	    
}



$(function(){
	
	//初始化
	MenuPage.init();
	
});
